package com.charles.seckillManagement.controller;

import com.charles.seckillComponentCommon.constants.CacheConstants;
import com.charles.seckillComponentCommon.controller.BaseController;
import com.charles.seckillComponentCommon.domain.AjaxResult;
import com.charles.seckillComponentCommon.domain.TableInfo;
import com.charles.seckillComponentCommon.exceptions.ServiceException;
import com.charles.seckillComponentRedis.service.RedisService;
import com.charles.seckillComponentSecurity.annotation.RolePermit;
import com.charles.seckillData.domain.SysGood;
import com.charles.seckillManagement.form.GoodListQuerys;
import com.charles.seckillManagement.mapper.SysGoodMapper;
import com.charles.seckillManagement.service.SysGoodService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/admin/good")
public class AdminGoodController extends BaseController {
    
    @Autowired
    private SysGoodMapper sysGoodMapper;
    
    @Autowired
    private SysGoodService sysGoodService;
    
    @Autowired
    private RedisService redisService;

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 产品列表
     */
    @GetMapping("getGoodTableList")
    @RolePermit(values = "admin")
    public TableInfo getGoodTableList(GoodListQuerys goodListQuerys) {
        startPage();
        return getDataTable(sysGoodMapper.queryGoodTableList(goodListQuerys));
    }

    /**
     * 获取产品
     */
    @GetMapping("/getGood/{goodId}")
    @RolePermit(values = "admin")
    public AjaxResult getGood(@PathVariable("goodId") Long goodId) {
        return AjaxResult.success(sysGoodMapper.getGood(goodId));
    }

    /**
     * 添加产品
     */
    @PostMapping("addGood")
    @RolePermit(values = "admin")
    @Transactional
    public void addGood(@RequestBody SysGood good) {
        sysGoodService.verifyGoodForm(good);
        
        RLock lock = redissonClient.getLock("addGood");
        
        try {
            boolean locked = lock.tryLock(10, 30, TimeUnit.SECONDS);
            if (locked) {
                sysGoodMapper.addGood(good);
                sysGoodService.checkGoodIsSeckill(sysGoodMapper.queryLastAddGood());
            } else {
                throw new ServiceException("抱歉，当前服务器人数过多，请稍后再试");
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ServiceException("抱歉，服务端异常，请稍后再试");
        } finally {
            if (lock != null && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
    
    /**
     * 修改产品
     */
    @PutMapping("modifyGood")
    @RolePermit(values = "admin")
    public void modifyGood(@RequestBody SysGood good) {
        sysGoodService.verifyGoodForm(good);
        sysGoodMapper.modifyGood(good);
        sysGoodService.checkGoodIsSeckill(good);
    }

    /**
     * 删除产品
     */
    @DeleteMapping("/deleteGood/{goodId}")
    @RolePermit(values = "admin")
    public void deleteGood(@PathVariable("goodId") Long goodId) {
        sysGoodMapper.deleteGood(goodId);
        String cacheKey = CacheConstants.GOOD_ID + goodId;
        if (redisService.getCacheObject(cacheKey) != null) {
            redisService.deleteObject(cacheKey);
        }
    }
    
}
